import java.util.ArrayList;
import java.util.Collections;

public class Main {
    public static void main(String[] args) {
        System.out.println("Pride and Prejudice");

        ArrayList<String> words = new ArrayList<>();

        if (FileOperation.readFile("/Users/liuyifan/IdeaProjects/Data-Structures/12-AVLTree/pride-and-prejudice.txt", words)) {
            // 先按照字母序排序一下，为了使二分搜索树退化为链表
            Collections.sort(words);
            // Test BST
            long startTime = System.nanoTime();

            AVLTree<String, Integer> bst = new AVLTree<>();
            for (String word : words) {
                if (bst.contains(word)) {
                    bst.set(word, bst.get(word) + 1);
                } else {
                    bst.add(word, 1);
                }
            }

            for (String word : words) {
                bst.contains(word);
            }
            long endTime = System.nanoTime();

            double time = (endTime - startTime) / 1000000000.0;
            System.out.println("AVL : " + time + " s");

            // Test AVL Tree
            startTime = System.nanoTime();

            RBTree<String, Integer> rbt = new RBTree<>();
            for (String word : words) {
                if (rbt.contains(word)) {
                    rbt.set(word, rbt.get(word) + 1);
                } else {
                    rbt.add(word, 1);
                }
            }

            for (String word : words) {
                rbt.contains(word);
            }
            endTime = System.nanoTime();

            time = (endTime - startTime) / 1000000000.0;
            System.out.println("RB : " + time + " s");
        }
    }
}
